package com.xiaohudie.SGGJUC;

import java.util.concurrent.*;

/**
 * @Author xiao hu die
 * @Date 2022/3/4 22:06
 * @Version 1.0
 */
class Num{
    private Integer number =0;
    /**加1的方法*/
    public  synchronized  void add() throws InterruptedException {
        while (number ==0 ){
            number++;
            this.notifyAll();
            System.out.println(Thread.currentThread().getName()+"number是:"+number);
        }
            this.wait();
    }
    /**减1的方法*/
    public synchronized void del() throws InterruptedException {
        while (number==1){
            number--;
            this.notifyAll();
            System.out.println(Thread.currentThread().getName()+"number是:"+number);
        }
            this.wait();

    }
}
public class TestCommunication {
    public static void main(String[] args) {
        Num num = new Num();
        new Thread(() -> {
            for (int i = 0; i <=5 ; i++) {
            try {
                Thread.sleep(500);
                num.add();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }}
        }, "AA").start();
        new Thread(() -> {
            for (int i = 0; i <=5 ; i++) {
            try {
                Thread.sleep(500);
                num.del();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }}
        }, "BB").start();
    }
}
